define(function(require,exports,module){
	var ELView = require("elview");
	var Dialog = require("dialog");
	require("./assets/css/groupAppoint_main.css");
	
	//component
	var cSubnav = require("./component/groupAppointSubnav");
	var cGrid = require("./component/groupAppointGrid");
	var cForm = require("./component/groupAppointForm");
	
	//handler
	var handler = require("./handler/groupAppointHandler");
	
	//service
	var service = require("./service/groupAppointSvc");
	
	//store
	var store = require("./store/groupAppointStore");
	
	var App = ELView.extend({
		attrs: {
			template: require("./assets/tpl/groupAppoint_main.tpl"),
			model: {
				datas: {},
				selectedDatas: {}
			}
		},
		events: {
			"click .J-status": function(e){
				var el = $(e.currentTarget);
				$(".J-status").removeClass("onClick");
				el.addClass("onClick");
				//过滤选择范围
				var key = "";
				if(el.hasClass("clear")){
					key = "clear";
				}else if(el.hasClass("dirty")){
					key = "dirty";
				}else if(el.hasClass("checkin")){
					key = "checkin";
				}else if(el.hasClass("willgo")){
					key = "willgo";
				}else if(el.hasClass("appoint")){
					key = "appoint";
				}else if(el.hasClass("repairing")){
					key = "repairing";
				}else if(el.hasClass("temporary")){
					key = "temporary";
				}else if(el.hasClass("selfuse")){
					key = "selfuse";
				}else if(el.hasClass("free")){
					key = "free";
				}
				$(".roomBox").addClass("hidden");
				$(".roomBox." + key).removeClass("hidden");
			},
			"click .J-roomBox": function(e){
				
				if($(e.currentTarget).attr('data-isAppoint') == "true"){
					Dialog.alert({
						content:"该房间已被预约！"
					});
					return;
				}
				
				var subnav = this.get("subnav");
				var model = this.get("model");
				var datas = model.datas;
				var roomTypeNum = model.roomTypeNum;
				var img = $(e.currentTarget).find("img");
				var key = $(e.currentTarget).attr("data-key");
				var src = img.attr("src");
				if(src == "./assets/eling/elcms/travel/groupAppoint/src/assets/img/check.png"){
					this.removeData(key);
					img.attr("src","./assets/eling/elcms/travel/groupAppoint/src/assets/img/notselected.png")
				}else{
					this.addData(key,datas[key]);
					img.attr("src","./assets/eling/elcms/travel/groupAppoint/src/assets/img/check.png")
				}
				this.renderPartial(".J-selectedData");
			},
			"click .J-confirmToCard": function(){
				this.handler.appointComp.handleConfirmBtnClick(this);
			},
			"click .J-returnToCard": function(){
				this.handler.appointComp.handleCancelBtnClick(this);
			}
		},
		addData: function(pkRoom,data){
			var subnav = this.get("subnav");
			var model = this.get("model");
			var selectedDatas = model.selectedDatas;
			var roomType = subnav.getValue("roomTypes");
			if(!selectedDatas[roomType]){
				selectedDatas[roomType] = {
					text: subnav.getPlugin("roomTypes").getText(),
					buildings: {},
					length: 0
				}
			}
			var building = subnav.getValue("buildings");
			if(!selectedDatas[roomType].buildings[building]){
				selectedDatas[roomType].buildings[building] = {
					text: subnav.getPlugin("buildings").getText(),
					rooms: {}
				};
			}
			selectedDatas[roomType].buildings[building].rooms[pkRoom] = data;
			if(selectedDatas[roomType].buildings[building].rooms[pkRoom]){
				selectedDatas[roomType].length += 1;
			}
			
		},
		removeData: function(pkRoom){
			var subnav = this.get("subnav");
			var model = this.get("model");
			var selectedDatas = model.selectedDatas;
			var roomType = subnav.getValue("roomTypes");
			var building = subnav.getValue("buildings");
			delete selectedDatas[roomType].buildings[building].rooms[pkRoom];
			selectedDatas[roomType].length -= 1;
		},
		handler: handler,
		service: service,
		store: store,
		initComponent: function(params,widget){
			cSubnav.initComponent(widget);
			cGrid.initComponent(widget);
			cForm.initComponent(widget);
		},
		afterInitComponent: function(params,widget){
			widget.get("grid").refresh();
		},
		toList: function(){
			var subnav = this.get("subnav");
			subnav.hide(["return"]).show(["alliance","servicePoint","destination","time"]);
			this.hide([".J-form"]).show([".J-grid"]);
		},
		toCard: function(){
			var subnav = this.get("subnav");
			subnav.hide(["alliance","servicePoint","destination",
			             "time","buildings","roomTypes","appointTime"]).show(["return"]);
			this.show([".J-form"]).hide([".J-grid",".J-detail"]);
			// 入住联盟则，楼宇和房间为非必填，反之则反
			$(".J-form textarea[name='allianceBuilding']").attr("data-rule-required", true);
			$("div[el-data-component='allianceBuilding']").next().show();
			$(".J-form textarea[name='rooms']").attr("data-rule-required", true);
			$("div[el-data-component='rooms']").next().show();
		},
		toAppoint: function(){
			var subnav = this.get("subnav");
			subnav.hide(["return"]).show(["buildings","roomTypes","appointTime"]);
			this.show([".J-detail"]).hide([".J-form"]);
		}
	});
	
	module.exports = App;
});